package com.kwedding.service.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.kwedding.bean.TB_City;
import com.kwedding.bean.TB_Permission;
import com.kwedding.bean.TB_User;
import com.kwedding.bean.TB_UserPermission;
import com.kwedding.dao.TB_CityMapper;
import com.kwedding.dao.TB_PermissionMapper;
import com.kwedding.dao.TB_UserMapper;
import com.kwedding.dao.TB_UserPermissionMapper;
import com.kwedding.dto.web.RequestWebAddUser;
import com.kwedding.dto.web.RequestWebGetUserList;
import com.kwedding.dto.web.RequestWebInsertPermission;
import com.kwedding.dto.web.RequestWebLogin;
import com.kwedding.dto.web.RequestWebPermissionByUserId;
import com.kwedding.dto.web.RequestWebPermissionList;
import com.kwedding.dto.web.RequestWebUpdateUser;
import com.kwedding.dto.web.ResponseWebLogin;
import com.kwedding.dto.web.ResponseWebPermissionByUserId;
import com.kwedding.dto.web.ResponseWebPermissionList;
import com.kwedding.dto.web.ResponseWebPermissionSimpleList;
import com.kwedding.dto.web.ResponseWebUserList;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyPageInfo;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.IDGenerate;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;
import com.kwedding.util.Resp_PageInfo;

@Service
public class WebUserService extends LoggerParent {
	@Autowired
	private TB_UserMapper tb_UserMapper;
	@Autowired
	private TB_UserPermissionMapper tb_UserPermissionMapper;
	@Autowired
	private TB_PermissionMapper tb_PermissionMapper;
	@Autowired
	private TB_CityMapper tb_CityMapper;

	/**
	 * web端获取权限列表
	 * 
	 * @param permissionList
	 */
	@Transactional(readOnly = true)
	public Resp_PageInfo<ResponseWebPermissionList> getAllPermission(RequestWebPermissionList permissionList) {
		int page = 1;
		if (permissionList.getPageNum() != null) {
			page = permissionList.getPageNum();
		}
		//PageHelper.startPage(page, Constants.PAGESIZE);
		List<TB_Permission> permissions = tb_PermissionMapper.selectAllPermissionList(permissionList.getStatus());
		List<ResponseWebPermissionList> permissionLists = new ArrayList<>();
		if (permissions != null && permissions.size() > 0) {
			permissions.stream().forEach(r -> {
				ResponseWebPermissionList responseWebPermissionList = CopyerObject.copyerObject(r,
						ResponseWebPermissionList.class);
				responseWebPermissionList.setIdStr(r.getPermissionId().toString());
				permissionLists.add(responseWebPermissionList);
			});
		}
		return CopyPageInfo.copyPageInfo(permissions, permissionLists);
	}

	/**
	 * 添加权限
	 * 
	 * @param insertPermission
	 */
	@Transactional
	public void inserPermission(RequestWebInsertPermission insertPermission) {
		CheckedUtil.isNull(insertPermission.getPermissionName(), Constants.REQUEST_ERROR_1, "权限名称不能为空");
		CheckedUtil.isNull(insertPermission.getPermissionValue(), Constants.REQUEST_ERROR_2, "权限值不能为空");
		TB_Permission tb_Permission = CopyerObject.copyerObject(insertPermission, TB_Permission.class);
		tb_Permission.setPermissionId(IDGenerate.generate());
		tb_Permission.setStatus(false);
		tb_Permission.setCreateTime(new Date());
		tb_Permission.setUpdateTime(new Date());
		if (tb_PermissionMapper.insertPermission(tb_Permission) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "添加权限失败");
		}
	}

	/**
	 * 后台用户登录
	 * 
	 * @param webLogin
	 * @return
	 */
	@Transactional(readOnly = true)
	public ResponseWebLogin login(RequestWebLogin webLogin) {
		CheckedUtil.isNull(webLogin.getUserName(), Constants.REQUEST_ERROR_1, "用户名不能为空");
		CheckedUtil.isNull(webLogin.getPassword(), Constants.REQUEST_ERROR_2, "密码不能为空");
		TB_User tb_User = tb_UserMapper.login(webLogin.getUserName());
		if (tb_User == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "用户名或者密码错误");
		}
		if (!tb_User.getPassword().equals(webLogin.getPassword())) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_4, "账号密码错误");
		}
		if (tb_User.getStatus() != false) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_5, "账号状态不可使用");
		}
		List<String> permission_values = tb_UserPermissionMapper.selectPermissionByuserId(tb_User.getUserId());
		ResponseWebLogin responseWebLogin = CopyerObject.copyerObject(tb_User, ResponseWebLogin.class);
		responseWebLogin.setValues(permission_values);
		responseWebLogin.setIdStr(tb_User.getUserId().toString());
		responseWebLogin.setCityId(String.valueOf(tb_User.getCityId()));
		return responseWebLogin;
	}

	/**
	 * 获取权限
	 * 
	 * @param permissionByUserId
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<ResponseWebPermissionByUserId> getPermissionByUserId(RequestWebPermissionByUserId permissionByUserId) {
		CheckedUtil.isNull(permissionByUserId.getUserId(), Constants.REQUEST_ERROR_1, "用户不能为空");
		List<TB_UserPermission> tb_Permissions = tb_UserPermissionMapper
				.selectUserPermission(permissionByUserId.getUserId());
		List<ResponseWebPermissionByUserId> rePermissionByUserIds = new ArrayList<>();
		if (tb_Permissions != null && tb_Permissions.size() > 0) {
			tb_Permissions.stream().forEach(r -> {
				ResponseWebPermissionByUserId byUserId = new ResponseWebPermissionByUserId();
				TB_Permission tb_Permission = tb_PermissionMapper.selectByPrimaryKey(r.getPermissionId());
				if (tb_Permission != null) {
					byUserId.setPermissionName(tb_Permission.getPermissionName());
					byUserId.setPermissionValue(tb_Permission.getPermissionValue());
				}
				TB_User tb_User = tb_UserMapper.selectByPrimaryKey(permissionByUserId.getUserId());
				if (tb_User != null && tb_User.getType() == 1) {
					byUserId.setCityId(tb_User.getCityId().toString());
				}
				rePermissionByUserIds.add(byUserId);
			});
		}
		return rePermissionByUserIds;
	}

	/**
	 * 获取权限列表
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<ResponseWebPermissionSimpleList> getPermissionList() {
		List<TB_Permission> tb_Permissions = tb_PermissionMapper.selectPermissionList();
		List<ResponseWebPermissionSimpleList> permissionLists = new ArrayList<>();
		if (tb_Permissions != null && tb_Permissions.size() > 0) {
			tb_Permissions.stream().forEach(r -> {
				ResponseWebPermissionSimpleList permissionList = new ResponseWebPermissionSimpleList();
				permissionList.setIdStr(r.getPermissionId().toString());
				permissionList.setPermissionName(r.getPermissionName());
				permissionLists.add(permissionList);
			});
		}
		return permissionLists;
	}

	/**
	 * 添加用户
	 * 
	 * @param addUser
	 */
	@Transactional
	public void insertUser(RequestWebAddUser addUser) {
		CheckedUtil.isNull(addUser.getUserName(), Constants.REQUEST_ERROR_1, "用户名不能为空");
		CheckedUtil.isNull(addUser.getPassword(), Constants.REQUEST_ERROR_2, "密码不能为空");
		CheckedUtil.isNull(addUser.getType(), Constants.REQUEST_ERROR_3, "账户类型不能为空");
		CheckedUtil.isNullAndSize(addUser.getPermissionIds(), Constants.REQUEST_ERROR_4, "请为用户添加权限");
		if (addUser.getType() == 1) {
			CheckedUtil.isNull(addUser.getCityId(), Constants.REQUEST_ERROR_5, "区域管理员城市不能为空");
		}
		TB_User tb_User = CopyerObject.copyerObject(addUser, TB_User.class);
		tb_User.setUserId(IDGenerate.generate());
		tb_User.setStatus(false);
		tb_User.setCreateTime(new Date());
		tb_User.setUpdateTime(new Date());
		// 添加后台用户
		if (tb_UserMapper.insert(tb_User) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_6, "添加用户失败");
		}
		// 添加用户权限
		List<TB_UserPermission> tb_UserPermissions = new ArrayList<>();
		addUser.getPermissionIds().stream().forEach(r -> {
			TB_UserPermission tb_UserPermission = new TB_UserPermission();
			tb_UserPermission.setUserPermissionId(IDGenerate.generate());
			tb_UserPermission.setUserId(tb_User.getUserId());
			tb_UserPermission.setPermissionId(Long.parseLong(r));
			tb_UserPermission.setStatus(false);
			tb_UserPermission.setCreateTime(new Date());
			tb_UserPermission.setUpdateTime(new Date());
			tb_UserPermissions.add(tb_UserPermission);
		});
		if (tb_UserPermissionMapper.insertuserpermission(tb_UserPermissions) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_7, "添加用户权限失败");
		}
	}

	/**
	 * 获取用户列表
	 * 
	 * @param webUserList
	 */
	@Transactional(readOnly = true)
	public Resp_PageInfo<ResponseWebUserList> getUserList(RequestWebGetUserList getUserList) {
		int page = 1;
		if (getUserList.getPageNum() != null) {
			page = getUserList.getPageNum();
		}
		List<TB_User> tb_Users = tb_UserMapper.selectAllUserList(getUserList.getSearchName(), getUserList.getStatus(),
				getUserList.getType());
		List<ResponseWebUserList> responseWebUserLists = new ArrayList<>();
		if (tb_Users != null && tb_Users.size() > 0) {
			tb_Users.stream().forEach(r -> {
				ResponseWebUserList responseWebUserList = CopyerObject.copyerObject(r, ResponseWebUserList.class);
				responseWebUserList.setIdStr(r.getUserId().toString());
				TB_City tb_City = tb_CityMapper.selectByPrimaryKey(r.getCityId());
				if (tb_City != null) {
					responseWebUserList.setCityName(tb_City.getCityName());
				}
				// 设置用户的权限
				List<String> permissionIds = tb_UserPermissionMapper.selectPermissionByuserId(r.getUserId());
				responseWebUserList.setPermissions(permissionIds);
				responseWebUserLists.add(responseWebUserList);
			});
		}
		return CopyPageInfo.copyPageInfo(tb_Users, responseWebUserLists);
	}

	/**
	 * 修改用户信息
	 * 
	 * @param webUpdateUser
	 */
	@Transactional
	public void updateUser(RequestWebUpdateUser webUpdateUser) {
		CheckedUtil.isNull(webUpdateUser.getUserId(), Constants.REQUEST_ERROR_1, "请选择要修改的用户");
		TB_User tb_User = tb_UserMapper.selectByPrimaryKey(webUpdateUser.getUserId());
		if (tb_User == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "用户信息不存在");
		}
		tb_User = CopyerObject.copyerObject(webUpdateUser, TB_User.class);
		if (tb_UserMapper.updateSelectActive(tb_User) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "修改用户信息失败");
		}
		// 修改权限
		if (webUpdateUser.getPermissionIds() != null && webUpdateUser.getPermissionIds().size() > 0) {
			// 1.删除之前的权限 2.重新添加权限
			List<TB_UserPermission> tb_UserPermissions = new ArrayList<>();
			tb_UserPermissionMapper.downPermissionByUserId(webUpdateUser.getUserId());
			webUpdateUser.getPermissionIds().stream().forEach(r -> {
				TB_UserPermission tb_UserPermission = new TB_UserPermission();
				tb_UserPermission.setUserPermissionId(IDGenerate.generate());
				tb_UserPermission.setUserId(webUpdateUser.getUserId());
				tb_UserPermission.setPermissionId(Long.parseLong(r));
				tb_UserPermission.setStatus(false);
				tb_UserPermission.setCreateTime(new Date());
				tb_UserPermission.setUpdateTime(new Date());
				tb_UserPermissions.add(tb_UserPermission);
			});
			if (tb_UserPermissionMapper.insertuserpermission(tb_UserPermissions) < 1) {
				throw new RequestErrorBackException(Constants.REQUEST_ERROR_4, "修改用户权限失败");
			}
		}
	}
}
